function [feaScore, feaIdx] = fs_unsup_lkrscore(X, k)

[nSmp, nDim] = size(X);

Xnorm = sum(X.^2, 2);
X = bsxfun(@rdivide, X, sqrt(Xnorm));

G = computeGraphViaLocalRegression(X, k);

feaScore = zeros(nDim, 1);
for i1 = 1:nDim;
    Gi = computeGraphViaLocalRegression(X(:,i1), k);
    feaScore(i1) = sum(sum(abs(G - Gi)));
end

[feaScore, feaIdx] = sort(feaScore, 'ascend');
end

function G = computeGraphViaLocalRegression(X, k)
[nSmp, nDim] = size(X);
%****************************************************
% Compute the affinity graph via local regression
%****************************************************
Xnorm = sum(X.^2, 2);
KX = X * X'; % linear kernel
KX = (KX + KX')/2;
XD = bsxfun(@plus, Xnorm, Xnorm') - 2 * KX;
[~, Idx] = sort(XD, 2, 'ascend');
Idx = Idx(:, 2:k+1);
A = zeros(nSmp);
A(sub2ind([nSmp, nSmp], repmat([1:nSmp]', k, 1), Idx(:))) = 1;
A = max(A, A');
%KA = k .* A;
KA = 1 .* A;
G = bsxfun(@rdivide, KA, sum(KA, 2));
end
